<?php

namespace App\Http\Controllers\Admin;

use App\Models\Brand;
use App\Models\BucketCategory;
use App\Models\Goods;
use App\Models\GoodsCategory;
use App\Models\Picture;
use App\Models\ScoreGoods;
use App\Models\Shop;
use App\Models\ShopGoods;

/**
 * 商品管理控制器
 */
class GoodsController extends BaseController
{

    public function __construct(
        Goods $goods,
        Shop $shop,
        GoodsCategory $goodsCategory,
        Brand $brand,
        Picture $picture,
        BucketCategory $bucketCategory,
        ShopGoods $shopGoods,
        ScoreGoods $scoreGoods
    )
    {
        parent::__construct();
        $this->goods = $goods;
        $this->shopGoods = $shopGoods;
        $this->shop = $shop;
        $this->goodsCategory = $goodsCategory;
        $this->bucketCategory = $bucketCategory;
        $this->brand = $brand;
        $this->picture = $picture;
        $this->scoreGoods = $scoreGoods;
    }


    public function getIndex()
    {
        $shopList = $this->shop->_getList(['admin_id' => is_login()])->toArray();
        $rows = $this->goods->getOwnGoodsList();
        if (request()->has('shop_id')) {
            foreach ($shopList as $v) {
                if ($v['id'] == request('shop_id')) {
                    $curentShop = $v;
                }
            }
        }
        $ext = $this->picture->config['saveExt'];
        $mini = 60;
        if ($this->picture->config['cut_Mini_Size']) {
            $miniArr = explode(',', $this->picture->config['cut_Mini_Size']);
            $mini = $miniArr[0];
        }
        return view('admin.goods.index', compact('rows', 'shopList', 'curentShop', 'ext', 'mini'));
    }


    public function getInfo()
    {
        $info = $this->goods->_getOneById(request('id'));
        return view('admin.goods.info', compact('info'));
    }

    /* 新增分类 */
    public function getAdd()
    {
        $goodsCategory = $this->goodsCategory->getCategoryTree();
        unset($goodsCategory[0]);
        $brand = $this->brand->_getList([], '*', ['sort' => 'asc'])->toArray();
        $bucketCategory = $this->bucketCategory->_getList(['status'=>1,'admin_id'=>is_login()], '*', ['sort' => 'asc'])->toArray();
        $ext = $this->picture->config['saveExt'];
        return view('admin.goods.edit', compact('goodsCategory', 'bucketCategory', 'brand', 'ext'));
    }

    public function postAdd()
    {
        $res = $this->goods->addOrUpdate();
        if (!$res) {
            return $this->error($this->goods->getError() ? $this->goods->getError() : '未知错误');
        }
        return $this->success('新增成功', urlAdmin('goods', 'getIndex', ['shop_id' => request('shop_id')]));
    }

    /* 编辑分类 */
    public function getEdit()
    {
        $goodsCategory = $this->goodsCategory->getCategoryTree();
        unset($goodsCategory[0]);
        $brand = $this->brand->_getList([], '*', ['sort' => 'asc'])->toArray();
        $info = $this->goods->_getOneById(request('id'));
        $info = $info ? $info->toArray() : [];
        $bucketCategory = $this->bucketCategory->_getList(['status'=>1,'admin_id'=>is_login()], '*', ['sort' => 'asc'])->toArray();
        $ext = $this->picture->config['saveExt'];
        return view('admin.goods.edit', compact('goodsCategory', 'bucketCategory', 'brand', 'info', 'ext'));
    }

    public function postEdit()
    {
        $res = $this->goods->addOrUpdate();
        if (!$res) {
            return $this->error($this->goods->getError() ? $this->goods->getError() : '未知错误');
        }
        return $this->success('更新成功', urlAdmin('goods', 'getIndex', ['shop_id' => request('shop_id')]));
    }

    //上传
    public function postUpload()
    {
        $res = $this->goods->uploadPicture();
        return $this->ajaxReturn($res);
    }

    /**
     * 管理员状态修改
     */
    public function anyChangeStatus()
    {
        $method = request('method');
        $ids = array_unique((array)request('ids'));
        if (empty($ids)) {
            return $this->error('请选择要操作的数据!');
        }
        if (!is_array($ids)) {
            $ids = [$ids];
        }
        $where['id'] = ['in', $ids];
        switch (strtolower($method)) {
            case 'up':
                return $this->resume($this->goods, $where, ['success' => '上架成功！', 'error' => '上架失败！']);
            case 'down':
                return $this->delete($this->goods, $where, ['success' => '下架成功！', 'error' => '下架失败！']);
            default:
                return $this->error('参数非法');
        }
    }

    public function getScoreGoods()
    {
        $where = [];
        if(!is_administrator()) {
            $where['admin_id'] = plat_admin_id();
        }
        if(request('shop_id')) {
            $where['shop_id'] = request('shop_id');
        }
        $rows = $this->scoreGoods->getScoreGoodsList($where);

        $ext = $this->picture->config['saveExt'];
        $mini = 60;
        if ($this->picture->config['cut_Mini_Size']) {
            $miniArr = explode(',', $this->picture->config['cut_Mini_Size']);
            $mini = $miniArr[0];
        }

        $shopWhere = [];
        if(isset($where['admin_id'])) {
            $shopWhere['admin_id'] = $where['admin_id'];
        }
        $shops = $this->shop->_getList($shopWhere);
        $ctr = 'goods';
        return view('admin.score_goods.goods', compact('rows','shops', 'mini', 'ext','ctr'));
    }

}
